DROP PROCEDURE IF EXISTS CREATE_TABLE;
DELIMITER $$


CREATE PROCEDURE `CREATE_TABLE`()
BEGIN
<#if tableList??&&(tableList?size>0)>
    <#list tableList as t>
        IF NOT EXISTS (select 1 from information_schema.tables where table_name ='${t.name}')
        THEN
        create table ${t.name}(
        <#if t.tableColumns??&&(t.tableColumns?size>0)>
            <#list t.tableColumns as column>
                <#if column_index!=0>
                    ,
                </#if>
                <#if column.columnName??&&column.columnName!="">
                     `${column.columnName}`
                </#if>
                ${column.columnMySQLType()}
                <#if column.columnLength??&&column.columnLength!="">
                     (${column.columnLength}
                </#if>
                <#if column.columnPrecision??&&column.columnPrecision!="">
                     ,${column.columnPrecision})
                <#elseif column.columnLength??&&column.columnLength!="">
                     )
                </#if>
                <#if column.columnDefault??&&column.columnDefault!="">
                      default ${column.columnDefault}
                </#if>
                <#if column.getNullable()>
                      NOT NULL
                </#if>
                <#if column.columnAnnotation??&&column.columnAnnotation!="">
                     COMMENT '${column.columnAnnotation} '
                </#if>
            </#list>
            <#if t.tablePrimaryKeys?? && (t.tablePrimaryKeys?size > 0) >
                <#list t.tablePrimaryKeys as primaryKey>
                    <#if primaryKey.keyType=="PRIMARY_KEY">
                        , PRIMARY KEY (${primaryKey.primaryKeyColumn})
                    </#if>
                    <#if primaryKey.keyType=="UNIQUE">
                        ,unique key(${primaryKey.primaryKeyColumn})
                    </#if>
                </#list>
            </#if>
        </#if>
        )DEFAULT CHARSET=GBK
        <#if t.tableAnnotation??&&t.tableAnnotation!="">
            COMMENT='${t.tableAnnotation}'
        </#if>;
        END IF;

    </#list>
</#if>
END;
CALL CREATE_TABLE;
DROP PROCEDURE CREATE_TABLE;

$$